package org.ThreadDemo.sync;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

class Mycache {
    private volatile Map<String,Object> map  = new HashMap<>();

    public void put(String key, Object value){

        System.out.println(Thread.currentThread().getName()+"正在写操作"+key);
        try {
            TimeUnit.MICROSECONDS.sleep(500);

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        map.put(key,value);

        System.out.println(Thread.currentThread().getName()+"写完了"+key);
    }

    public Object get(String key){
        Object result = null;
        System.out.println(Thread.currentThread().getName()+"正在读取操作"+key);

        try {
            TimeUnit.MICROSECONDS.sleep(500);

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        result = map.get(key);
        System.out.println(Thread.currentThread().getName()+"取完了"+key);
        return result;
    }
}
public class ReadWriteLockDemo {
    public static void main(String[] args) {

        Mycache myCache = new Mycache();

        for(int i =1;i<=5; i++){
            final int num = 1;
            new Thread(
                    ()->{
                        myCache.put(num+"",num+"");
                    },String.valueOf(i)
            ).start();
        }

        for(int i =1;i<=5; i++){
            final int num = 1;
            new Thread(
                    ()->{
                        myCache.get(num+"");
                    },String.valueOf(i)
            ).start();
        }


    }
}
